<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\HomeBaseController;
use app\portal\model\PortalCategoryModel;
use app\portal\service\PostService;
use app\portal\model\PortalPostModel;
use think\Db;

class ArticleController extends HomeBaseController
{
    public function index()
    {
    	//cookie('think_var', 'en-us');
        $portalCategoryModel = new PortalCategoryModel();
        $postService         = new PostService();

        $articleId  = $this->request->param('id', 0, 'intval');
        $categoryId = $this->request->param('cid', 0, 'intval');
        $article    = $postService->publishedArticle($articleId, $categoryId);

        if (empty($article)) {
            abort(404, '文章不存在!');
        }


        $prevArticle = $postService->publishedPrevArticle($articleId, $categoryId);
        $nextArticle = $postService->publishedNextArticle($articleId, $categoryId);

        $tplName = 'article';

        if (empty($categoryId)) {
            $categories = $article['categories'];

            if (count($categories) > 0) {
                $this->assign('category', $categories[0]);
            } else {
                abort(404, '文章未指定分类!');
            }

        } else {
            $category = $portalCategoryModel->where('id', $categoryId)->where('status', 1)->find();

            if (empty($category)) {
                abort(404, '文章不存在!');
            }

            $this->assign('category', $category);

            $tplName = empty($category["one_tpl"]) ? $tplName : $category["one_tpl"];
        }

        Db::name('portal_post')->where(['id' => $articleId])->setInc('post_hits');


        hook('portal_before_assign_article', $article);

        $this->assign('article', $article);
        $this->assign('prev_article', $prevArticle);
        $this->assign('next_article', $nextArticle);

        $tplName = empty($article['more']['template']) ? $tplName : $article['more']['template'];
      //  exit($tplName);
        if($articleId==4){
        	
        //	$list =  Db::name('volume')->query("select volume.*,category.name cname from web_volume_category category
    		//left join web_volume volume  on volume.cid=category.id order by category.sort asc,volume.sort asc ");
        	
        	$list =  Db::name('volume_category')->query("select * from web_volume_category category
    		order by category.sort asc ");
        	
        	foreach ( $list as &$val){
        		$val['info']=Db::name('volume')->query("select * from web_volume where cid='{$val['id']}'");
        	}
        	//print_r($list); die();
        	$this->assign('list', $list);
        	return $this->fetch("/article_volume");
        }else if($articleId==21){
        	return $this->fetch("/article_price");
        }else{
        	return $this->fetch("/$tplName");
        }
        
       
    }
    
    public function addPost()
    {
    	if ($this->request->isPost()) {
    		$data   = $this->request->param();
    		//print_r($data); die();
    		//  exit($data['photo_urls'][0]);
    		$data['ctime']=date('Y-m-d',time());
    		//  die();
    		Db::name('volume_info')->insert($data);
    		//$this->success('添加成功!', url('AdminVolume/add'));
    		$this->success('在线询价提交成功,请等待客服联系!', url('portal/article/index', ['id' => 21]));
    	}
    
    }

    // 文章点赞
    public function doLike()
    {
        $this->checkUserLogin();
        $articleId = $this->request->param('id', 0, 'intval');


        $canLike = cmf_check_user_action("posts$articleId", 1);

        if ($canLike) {
            Db::name('portal_post')->where(['id' => $articleId])->setInc('post_like');

            $this->success("赞好啦！");
        } else {
            $this->error("您已赞过啦！");
        }
    }

}
